<?php
/* @var $this TermsController */
/* @var $model Terms */

$this->breadcrumbs=array(
	'Terms'=>ApplicationConfig::getURL("", "terms", "admin",array('id'=>$_GET['id'])),
	'Manage',
);


 // $this->widget('bootstrap.widgets.TbMenu', array(
    // 'type'=>'tabs', // '', 'tabs', 'pills' (or 'list')
    // 'stacked'=>true, // whether this is a stacked menu
	// 'htmlOptions'=>array('style'=>'width:25%'),
    // 'items'=>array(
 	// array('label'=>'Create Term', 'url'=>ApplicationConfig::getURL("user", "terms", "create",array('id'=>$_GET['id']))),
    // array('label'=>'List Terms', 'url'=>ApplicationConfig::getURL("user", "terms", "admin",array('id'=>$_GET['id']))),
   
 // ),
// )); 

$this->menu=array(
	// array('label'=>'List EpisodeDetails','url'=>array('index')),
	array('label'=>'Create Term','url'=>ApplicationConfig::getURL("", "terms", "create",array('id'=>$_GET['id']))),
	array('label'=>'Manage Category','url'=>ApplicationConfig::getURL("", "categories", "admin",array())),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#terms-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");
?>
<?php 
if (!empty($_GET['id']))
{
	$categoryObj = Categories::model()->getModel("CATEGORY-ID",array('categoryID'=>$_GET['id']));
	$categoryName = $categoryObj->category_name;
}
else 
$categoryName = "";

?>
<h3>Manage Terms  - <?php echo $categoryName; ?></h3>

<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php /*$this->renderPartial('_search',array(
	'model'=>$model,
)); */?>
</div><!-- search-form -->
<?php
 
 	Yii::app()->clientScript->registerScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
	Yii::app()->clientScript->registerScriptFile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js');
    $str_js = "
        var fixHelper = function(e, ui) {
            ui.children().each(function() {
                $(this).width($(this).width());
            });
            return ui;
        };
 
        $('#terms-grid table.items tbody').sortable({
            forcePlaceholderSize: true,
            forceHelperSize: true,
            items: 'tr',
            update : function () {
                serial = $('#terms-grid table.items tbody').sortable('serialize', {key: 'items[]', attribute: 'class'});
                $.ajax({
                    'url': '" . ApplicationConfig::getURL("", "terms", "sort") . "',
                    'type': 'post',
                    'data': serial,
                    'success': function(data){
                    },
                    'error': function(request, status, error){
                        alert('We are unable to set the sort order at this time.  Please try again in a few minutes.');
                    }
                });
            },
            helper: fixHelper
        }).disableSelection();
    ";
 
    Yii::app()->clientScript->registerScript('sortable-project', $str_js);
?>
<?php $this->widget('bootstrap.widgets.TbGridView', array(
	'id'=>'terms-grid',
	'dataProvider'=>$model->search($_GET['id']),
	'filter'=>$model,
	'type'=>'striped bordered condensed hover',
	'rowCssClassExpression'=>'"items[]_{$data->termID}"',
	'columns'=>array(
		'termID',
		array(
		'name'=>'categoryID',
		'value'=>'Categories::model()->getModel("CATEGORY-ID",array("categoryID"=>$data->categoryID))->category_name'
		),
		'term_name',
		array(
		'header'=>'Parent name',
		'name'=>'parentID',
		'value'=>'($data->parentID !="0") ? Terms::model()->getModel("TERM-ID",array("termID"=>$data->parentID))->term_name : ""',
		'filter'=>CHtml::listData(Terms::getList("PARENT-LIST",array("categoryID"=>$_GET['id'])), "termID", "term_name")
		),
		array(
			'name'=>'created',
			'value'=>'ApplicationConfig::app()->getFormattedDate($data->created)',
			'filter'=>false,
		),

		array(
			'name'=>'sortOrder',
			'type'  => 'raw',
			'value'=>'CHTML::textField($data->termID,$data->sortOrder,array(\'id\'=>$data->termID,\'onkeyup\'=>\'ajaxsubmitorder(this)\',\'style\'=>\'width:30px\'))',
			'filter'=>false,
		),
		array(
			'class'=>'bootstrap.widgets.TbButtonColumn',
			'template'=>'{update}{delete}'
		),
	),
)); ?>

<script>

function ajaxsubmitorder(control)
{
    var val=$(control).val();
    var id=$(control).attr('id');
	$.ajax({
	url:"<?php echo ApplicationConfig::getURL("user", "terms", "sortitems"); ?>",
	data:"val="+val+"&id="+id,
	type:'POST',
	success:function(result){

	}

	});
	 
}
</script>